﻿using Common.Contract.Warranty.Record;
using Common.Contract.Warranty.Request;
using Common.Contract.Warranty.Response;
using Common.Service.Command;
using Warranty.Model.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Warranty.ServiceCommand
{
    public class RetrieveResponseSMSServiceCommand : ProviderServiceCommand<IResponseSMSProvider, RetrieveResponseSMSRequest, RetrieveResponseSMSResponse>
    {

        public RetrieveResponseSMSServiceCommand(IResponseSMSProvider providers) : base(providers) { }

        protected override void Validate(RetrieveResponseSMSRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
        }

        protected override bool OnExecute(RetrieveResponseSMSResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            var oragnizationRecord = Providers.RetrieveResponseSMS(Request.RespType);
            if (oragnizationRecord == null)
            {
                response.ErrorMessage = string.Format("Can not found user with OrgCode={0} ", Request.RespType);
                response.IsSuccess = false;
                return false;
            }

            ResponseSMSRecord record = new ResponseSMSRecord
            {
                CreateDate = oragnizationRecord.CreateDate,
                GabitId = oragnizationRecord.GabitId,
                GabitTemplate = oragnizationRecord.GabitTemplate,
                MsgBodyLen = oragnizationRecord.MsgBodyLen,
                Purpose = oragnizationRecord.Purpose,
                RespMsg1 = oragnizationRecord.RespMsg1,
                RespMsg2 = oragnizationRecord.RespMsg2,
                RespType = oragnizationRecord.RespType,
                Template = oragnizationRecord.Template
            };
            response.Record = record;
            return isSuccess;
        }
    }
}
